﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CasaBella.Entities;
using CasaBella.BL;
using Microsoft.Practices.Unity;

namespace CasaBella.UI
{
    public partial class FrmMaterial : Form
    {
        [Dependency]
        public IMaterialBL materialBL { get; set; }

        public FrmMaterial()
        {
            InitializeComponent();
            this.BuilUp();
        }

        private void FrmMaterial_Load(object sender, EventArgs e)
        {
            cargaData();
        }

        public void cargaData()
        {
            mATERIALBindingSource.DataSource = materialBL.GetAllFromMaterial();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            mATERIALBindingSource.DataSource = materialBL.GetAllFromMaterialByCriterio(TxtCriterio.Text);
            if (mATERIALBindingSource.Count == 0)
            {
                MessageBox.Show("Material no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void TxtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                mATERIALBindingSource.DataSource = materialBL.GetAllFromMaterialByCriterio(TxtCriterio.Text);
                if (mATERIALBindingSource.Count == 0)
                {
                    MessageBox.Show("Material no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoMaterial frm = new FrmMttoMaterial();
            frm.NuevaMaterial();
            frm.ShowDialog();
            cargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var material = mATERIALBindingSource.Current as MATERIAL;

            if (material != null)
            {
                FrmMttoMaterial frm = new FrmMttoMaterial();
                frm._material = material;
                frm.EditarMaterial();
                frm.ShowDialog();
                cargaData();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Material.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var material = mATERIALBindingSource.Current as MATERIAL;

            if (material != null)
            {
                var rpt = MessageBox.Show(this,
                    string.Format("¿Desea Eliminar el Material: " + material.Descripcion + "?"),
                    "Mensaje",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                if (rpt == DialogResult.Yes)
                {
                    materialBL.DeleteMaterial(material.Id);
                    cargaData();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Material.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void TxtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
